今天來介紹 pandas 的另一個結構 Series 。
Series 是一個一維的陣列,裡面由index和value组成,其實 DataFrame 就是由多個 Series 所組成, DataFrame 裡面的任一行任一列都是一個 Series 。
我們可以自己創一個數據來演示。
import numpy as np
import pandas as pd
da = {
"Name":["王小明","王中明","王大明","陳小明","陳大明"],
"Age":["12","32",'34',"54","78"],
"Sex":["M","G","M","M","G"]
}
dt = pd.DataFrame(da)
dt
接著我們試試從 DataFrame 抓出一行數據出來。
import pandas as pd
import numpy as np
da = {
"Name":["王小明","王中明","王大明","陳小明","陳大明"],
"Age":["12","32",'34',"54","78"],
"Sex":["M","G","M","M","G"]
}
dt = pd.DataFrame(da)
SAge = pd.Series(dt["Age"])
print(SAge)
print(type(SAge))
我們把 DataFrame 裡的 Age 這一行取出來,接著我們把它打印出來,也順便把它的資料型態也印出來,結果如下圖。
我們可以看到這五個人的Age都印出來了,接著看它的資料型態的確為 Series 。
接著我們來試試看把多個 Series 變成一個 DataFrame 。
我們把原本的 DataFrame 分成三個 Series(N,A,S) ,最後再把它合並起來
import pandas as pd
import numpy as np
da = {
"Name":["王小明","王中明","王大明","陳小明","陳大明"],
"Age":["12","32",'34',"54","78"],
"Sex":["M","G","M","M","G"]
}
dt = pd.DataFrame(da)
N = pd.Series(dt["Name"])
A = pd.Series(dt["Age"])
S = pd.Series(dt["Sex"])
dd = pd.DataFrame([N,A,S],index=["Name","Age","Sex"]) #合併
dd = dd.T #因為創建完的DataFrame是顛倒的,所以用這個指令把它轉回來
print(N)
print(A)
print(S)
print(dd)
print(type(dd))